package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.application.sistercityapplication.dto.ContactDto;
import com.application.sistercityapplication.dto.GalleryDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.application.sistercityapplication.listadapter.ContactListAdapter;
import com.application.sistercityapplication.listadapter.GridViewCateImageAdapter;
import com.application.sistercityapplication.main.NoticeAndNewsFragment.DownLoadNewsTask;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

@SuppressLint("ValidFragment")
public class MainCatImageActivity extends Fragment {
	private View v,v2;
	private FrameLayout mainLayout;
	private LayoutInflater inflater2;
	private int cId;
	private SlidingMenu sm;
	private String countryTitle;
	private GridView gridView;
	private Utils utils;
	private int columnWidth;
	private GridViewCateImageAdapter adapter;
	private ProgressBar progress;
	private ImageView menuToggle,back_icon;
	
	public MainCatImageActivity(int country_id,String country_title,SlidingMenu sm){
		this.cId = country_id;
		this.countryTitle = country_title;
		this.sm = sm;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.new_activity_city_information_main, container,false);

		mainLayout = (FrameLayout) v.findViewById(R.id.frameForInflate);
		inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v2 = inflater2.inflate(R.layout.activity_main_cat_image, mainLayout);
		
		gridView = (GridView) v2.findViewById(R.id.grid_view);
		progress = (ProgressBar) v2.findViewById(R.id.progress);
		progress.setVisibility(View.GONE);
		menuToggle = (ImageView) v2.findViewById(R.id.menuToggle);
		menuToggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sm.toggle();
			}
		});
		back_icon = (ImageView) v2.findViewById(R.id.back_icon);
		back_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Fragment newContent = new MainPageFragment(cId,countryTitle,sm);
				switchFragment(newContent);
			}
		});
		utils = new Utils(getActivity().getApplicationContext());
		InitilizeGridLayout();
		new DownloadContactTask(getActivity().getApplicationContext(),getActivity()).execute();
		
		return v;
		
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	private void InitilizeGridLayout() {
		Resources r = getResources();
		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,AppConstant.GRID_PADDING, r.getDisplayMetrics());

		columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

		gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
		gridView.setColumnWidth(columnWidth);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setPadding((int) padding, (int) padding, (int) padding,(int) padding);
		gridView.setHorizontalSpacing((int) padding);
		gridView.setVerticalSpacing((int) padding);
	}
	public class AppConstant {

		// Number of columns of Grid View
		public static final int NUM_OF_COLUMNS = 3;

		// Gridview image padding
		public static final int GRID_PADDING = 20; // in dp

		// SD card image directory
		public static final String PHOTO_ALBUM = "NAT";

		// supported file formats
		public final List<String> FILE_EXTN = Arrays.asList("jpg", "jpeg","png");
	}
	public class DownloadContactTask extends AsyncTask<String, Void, List<GalleryDto>>{
		ProgressDialog pDialog;
		private Context context;
		public Activity _activity;
		public DownloadContactTask(Context context,Activity act) {
			super();
			this.context = context;
			this._activity = act;
		}

		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		@Override
		protected List<GalleryDto> doInBackground(String... params) {
			// TODO Auto-generated method stub
			JSONParserForGetList json = JSONParserForGetList.getInstance();
			return json.getGallerys(cId+"");
		}
		@Override
		protected void onPostExecute(List<GalleryDto> result) {
			super.onPostExecute(result);
			adapter = new GridViewCateImageAdapter(getActivity(),result,columnWidth,cId,countryTitle,sm);
			gridView.setAdapter(adapter);

		}
		
	}
}
